package api;

import com.fasterxml.jackson.databind.ObjectMapper;
import common.Util;
import dao.Blog;
import dao.BlogDao;
import dao.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

@WebServlet("/blog")
public class  BlogServlet extends HttpServlet {
    private ObjectMapper objectMapper=new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf8");
        User user= Util.checkLoginStatus(req);
        if (user==null){
            resp.setStatus(403);
            String html="<h3>当前用户未登录</h3>";
            resp.getWriter().write(html);
            return;
        }
//        req.setCharacterEncoding("utf8");
        String blogId=req.getParameter("blogId");
        BlogDao blogDao=new BlogDao();
        if (blogId==null){
            List<Blog> blogs=blogDao.selectAll();
            String jsonString=objectMapper.writeValueAsString(blogs);
            resp.getWriter().write(jsonString);
        }
        else {
            Blog blog=blogDao.selectOne(Integer.parseInt(blogId));
            String jsonString=objectMapper.writeValueAsString(blog);
            resp.getWriter().write(jsonString);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user=Util.checkLoginStatus(req);
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        if (user==null){
            String html="<h3>当前用户未登录</h3>";
            resp.getWriter().write(html);
            return;
        }
        String title=req.getParameter("title");
        String content=req.getParameter("content");
        if ("".equals(title)||title==null||"".equals(content)||content==null){
            String html="<h3>当前提交的内容缺失</h3>";
            resp.getWriter().write(html);
            return;
        }
        Blog blog=new Blog();
        blog.setContent(content);
        blog.setTitle(title);
        blog.setUserId(user.getUserId());
        blog.setPostTime(new Timestamp(System.currentTimeMillis()));
        BlogDao blogDao=new BlogDao();
        blogDao.insert(blog);
        resp.sendRedirect("blog_list.html");
    }
    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        User user= Util.checkLoginStatus(req);
        if (user==null){
            String html="<h3>当前用户未登录</h3>";
            resp.getWriter().write(html);
            return;
        }
        String blogId=req.getParameter("blogId");
        if (blogId==null||"".equals(blogId)){
            String html="<h3>参数缺失</h3>";
            resp.getWriter().write(html);
            return;
        }
        BlogDao blogDao=new BlogDao();
        Blog blog=blogDao.selectOne(Integer.parseInt(blogId));
        if (blog==null){
            String html="<h3>博客不存在</h3>";
            resp.getWriter().write(html);
            return;
        }
        if (blog.getUserId()!=user.getUserId()){
            String html="<h3>无法删除他人博客</h3>";
            resp.getWriter().write(html);
            return;
        }
        blogDao.delete(Integer.parseInt(blogId));
        resp.setStatus(200);
        String html="<h3>删除成功</h3>";
        resp.getWriter().write(html);
    }
}
